/**
 * 
 */
package uniandes.dermatologia.resources.dao;

import java.util.ArrayList;
import java.util.Date;

import org.droidpersistence.dao.DroidDao;
import org.droidpersistence.dao.TableDefinition;

import uniandes.dermatologia.resources.models.ExaminationModel;
import android.database.sqlite.SQLiteDatabase;

/**
 * @author Julio
 *
 */
public class ExaminationDao extends DroidDao<ExaminationModel, Long> {

	/**
	 * @param model
	 * @param tableDefinition
	 * @param database
	 */
	public ExaminationDao(TableDefinition<ExaminationModel> tableDefinition,
			SQLiteDatabase database) {
		super(ExaminationModel.class, tableDefinition, database);
	}
	
	public ExaminationModel getLast(SQLiteDatabase db)
	{
		ExaminationModel model = null;
		ArrayList<ExaminationModel> models = (ArrayList<ExaminationModel>) getAll();
		Date last_date_added = null;
		Date date;
		for(ExaminationModel m : models)
		{
			date = m.getDate_added();
			if(last_date_added == null || date.after(last_date_added))
			{
				last_date_added = date;
				model = m;
			}
		}
		return model;
	}

	public long getModelId(ExaminationModel examinationModel) {
		long id = -1;
		String[] clauseArgs = new String[]{examinationModel.getShape(), examinationModel.getNumber(), examinationModel.getDistribution(),
				examinationModel.getLesionType(), examinationModel.getBorder(), examinationModel.getBodyParts(), examinationModel.getSymptoms(),
				examinationModel.getPast(), String.valueOf(examinationModel.getCase_id())};
		if(getAll().size() > 0)
		{
			ExaminationModel model = getByClause("SHAPE = ? AND NUMBER_OF_LESIONS = ? AND DISTRIBUTION = ? "
					+ "AND LESION_TYPE = ? AND BORDER = ? AND BODY_PARTS = ? "
					+ "AND SYMPTOMS = ? AND PAST = ? AND CASE_ID = ?", clauseArgs);
			if(model != null)
			{
				id = model.getId();
			}
		}
				
		return id;
	}
}
